package com.mzbopen.rightbottommoreview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;

/**
 * 监听ScrollView滚动到顶部或者底部做相关事件拦截
 */
public class SmartScrollView extends HorizontalScrollView implements View.OnTouchListener {
    private int showEndPadding = 0;

    public void setShowEndPadding(int showEndPadding) {
        this.showEndPadding = showEndPadding;
    }

    private boolean isScrolledToBottom = false;

    public SmartScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setOnTouchListener(this::onTouch);
    }

    private TouchUpIsRightListener touchUpIsRightListener;
    private OverScrolledListener overScrolledListener;

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                if (isScrolledToBottom && touchUpIsRightListener != null)
                    touchUpIsRightListener.onTouchUpIsRightListener();
                break;
        }
        return false;
    }

    public interface TouchUpIsRightListener {
        void onTouchUpIsRightListener();

    }

    public interface OverScrolledListener {
        void onOverScrolledListener(int scrollX, int scrollY, boolean clampedX, boolean clampedY);

    }

    public void setTouchUpIsRightListener(TouchUpIsRightListener touchUpIsRightListener) {
        this.touchUpIsRightListener = touchUpIsRightListener;
    }

    public void setOverScrolledListener(OverScrolledListener overScrolledListener) {
        this.overScrolledListener = overScrolledListener;
    }

    @Override
    protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) {
        super.onOverScrolled(scrollX, scrollY, clampedX, clampedY);
        if (overScrolledListener != null) overScrolledListener.onOverScrolledListener(scrollX, scrollY, clampedX, clampedY);
        if (scrollX == 0) {
            isScrolledToBottom = false;
        } else {
            if (scrollX >= 0)
                isScrolledToBottom = clampedX;
        }
    }


    public boolean isScrolledToBottom() {
        return isScrolledToBottom;
    }
}